package com.dj.mingkong.portal.biz.controller.system;

import com.dj.mingkong.common.annotation.RequestLimit;
import com.dj.mingkong.common.infrastructure.base.dto.rest.RestResponse;
import com.dj.mingkong.common.microservice.UserContext;
import com.dj.mingkong.portal.biz.controller.system.vo.request.LoginVo;
import com.dj.mingkong.portal.biz.controller.system.vo.request.RegisterVo;
import com.dj.mingkong.portal.biz.controller.system.vo.request.UserUpdateVo;
import com.dj.mingkong.portal.biz.controller.system.vo.response.DictionaryVo;
import com.dj.mingkong.portal.biz.controller.system.vo.response.UserInfoVo;
import com.dj.mingkong.portal.biz.controller.system.vo.response.UserVo;
import com.dj.mingkong.portal.biz.service.system.DictionaryService;
import com.dj.mingkong.portal.biz.service.system.UserService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户管理
 *
 * @author zenglonghui
 * @date: 2022/8/17 23:00
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;

    @Resource
    private DictionaryService dictionaryService;

    /**
     * 获取验证码图片
     *
     * @param request  请求
     * @param response 返回
     */
    @GetMapping("/getVerifyCodeImage")
    public void getVerifyCodeImg(HttpServletRequest request, HttpServletResponse response) {
        userService.getVerifyCodeImage(request, response);
    }

    /**
     * 用户注册
     *
     * @param registerVo 实例对象
     * @return 是否成功，true-成功，false-失败
     * @throws RuntimeException registerVo为空的时候会抛出运行时异常
     */
    @PostMapping("/register")
    public RestResponse<Boolean> register(
            @RequestBody @Validated RegisterVo registerVo, HttpServletRequest request) {
        registerVo.setUsername(registerVo.getUsername().trim());
        userService.insert(registerVo, request);
        return RestResponse.buildSuccess();
    }

    /**
     * 用户登录
     *
     * @param loginVo 实例对象
     * @return 是否成功，true-成功，false-失败
     * @throws RuntimeException loginVo为空的时候会抛出运行时异常
     */
    @PostMapping("/login")
    public RestResponse<String> login(
            @RequestBody @Validated LoginVo loginVo, HttpServletRequest request) {
        return RestResponse.success(userService.login(loginVo, request));
    }

    /**
     * 获取登录用户的详情信息
     *
     * @return UserInfoVo
     */
    @GetMapping("info")
    public RestResponse<UserInfoVo> info() {
        final UserInfoVo userInfoVo = userService.info(UserContext.getUserId());
        final List<DictionaryVo> dictionaryVos = dictionaryService.listTree();
        userInfoVo.setDictionaryVoList(dictionaryVos);
        return RestResponse.success(userInfoVo);
    }

    /**
     * 登出（注销）
     *
     * @return Boolean
     */
    @PostMapping("/logout")
    public RestResponse<Boolean> logout() {
        userService.logout();
        return RestResponse.buildSuccess();
    }

    /**
     * 获取我的信息
     *
     * @return UserVo
     */
    @GetMapping("getInfo")
    public RestResponse<UserVo> getInfo() {
        return RestResponse.success(userService.getInfo(UserContext.getUserId()));
    }

    /**
     * 修改用户信息
     *
     * @param updateVo {}
     * @return Boolean
     */
    @PostMapping("/updateById")
    @RequestLimit(count = 5)
    public RestResponse<Boolean> updateById(@RequestBody @Validated UserUpdateVo updateVo) {
        userService.updateById(updateVo);
        return RestResponse.buildSuccess();
    }
}
